//
//  TeacherInfoViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/10.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "TeacherInfoViewController.h"
#import "TeacherInfoHeaderView.h"
#import "UserInfoModel.h"
#import "TeacherEvalutionCell.h"
#import "TeacherOrderTimeCell.h"
#import "OrderTimeViewController.h"
#import "EvalutionTeacherController.h"
#import "ViewEvalutionTeacherController.h"


@interface TeacherInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

// 视图
@property(nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) TeacherInfoHeaderView *headerView;            // 导师头部view
@property (nonatomic, retain) UIView *tableViewHeader;
@property (nonatomic, retain) UILabel *teacherDetailLabel;                  // 导师详情
@property (nonatomic, retain)UIButton *orderTeacher;                        // 预约导师

// 数据
@property (nonatomic, retain) TeacherInfoModel *model;

@end

@implementation TeacherInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"导师详情";
    self.view.backgroundColor = UIColor.whiteColor;

    // 初始化视图
    [self setupView];
    
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 获取网络数据
        [self updateData];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}


#pragma mark - 自定义方法

-(void)setupView {
    // tableView header(导师介绍)
    self.tableViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];

    self.teacherDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80+10, SCREEN_WIDTH - 20, 0)];
    self.teacherDetailLabel.numberOfLines = 0;
    self.teacherDetailLabel.font = [UIFont systemFontOfSize:15];
    self.teacherDetailLabel.textColor = [UIColor grayColor];

    [self.tableViewHeader addSubview:self.teacherDetailLabel];




    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];

    self.tableView.tableHeaderView = self.tableViewHeader;




    // 导师header(导师名，类型，地点),自动计算高度
    self.headerView = [[TeacherInfoHeaderView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH,0)];
    [self.view addSubview:self.headerView];

    // 预约导师按钮
    self.orderTeacher = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-40, SCREEN_WIDTH, 40)];
    self.orderTeacher.backgroundColor = [UIColor orangeColor];
    [self.orderTeacher setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.orderTeacher setTitle:@"预约导师" forState:UIControlStateNormal];
    [self.orderTeacher addTarget:self action:@selector(orderTeacherTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.orderTeacher];
}

#pragma mark - 事件监听
-(void)orderTeacherTapped {
    OrderTimeViewController *orderTimeViewController = [[OrderTimeViewController alloc] init];
    orderTimeViewController.teacherID = self.teacherID;
    [self.navigationController pushViewController:orderTimeViewController animated:true];
}

#pragma mark - 网络连接

-(void)updateData {
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getTeacherInfo"
                                        parameters:@{@"s_id": [UserInfoModel shareInstance].stu_id, @"tea_id": self.teacherID}
                                           success:^(NSDictionary *respondDictionary) {
                                               self.model = [[TeacherInfoModel alloc] initWithDictionary:respondDictionary[@"teacherInfo"]];
                                               // 设置header模型
                                               self.headerView.model = self.model;


                                               // 设置导师介绍
                                               self.teacherDetailLabel.text = self.model.introduce;
                                               [self.teacherDetailLabel sizeToFit];
                                               self.tableViewHeader.height = self.teacherDetailLabel.bottomY + 10;

                                               self.tableView.tableHeaderView = self.tableViewHeader;

                                               [self.tableView reloadData];
                                               [self.tableView.mj_header endRefreshing];
                                           }
                                           failure:^(NSError *error) {
                                               
                                           }];



}



-(void)updateOrderTeacherWithModel:(TeacherOrderTimeModel *)model{
    [SVProgressHUD show];
    // 请求参数
    NSDictionary *parameters = @{@"s_id": [UserInfoModel shareInstance].stu_id, @"yy_id": @(model.yyid), @"code": @(model.code), @"type": @(1)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/appointTeacher"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               NSInteger code = [respondDictionary[@"code"] intValue];
                                               
                                               
                                               if(code == 3) {
                                                   // 取消预约成功
                                                   [SVProgressHUD showSuccessWithStatus:@"取消预约导师成功"];
                                                   [self.model.times removeObject:model];
                                                   [self.tableView reloadData];
                                               }else{
                                                   [SVProgressHUD showfailed];
                                               }
                                           } failure:^(NSError *error) {
                                               
                                               [SVProgressHUD showErrorWithStatus:@"预约失败"];
                                           }];
}


// 根据导师+时间评价
-(void)evalutionTeacherWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid{
    EvalutionTeacherController *controller = [[EvalutionTeacherController alloc] init];
    controller.teacherID = teacherID;
    controller.yyid = yyid;
    [self.navigationController pushViewController:controller animated:true];
}

// 查看已评价的详情
-(void)viewEvalutionWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid {
    ViewEvalutionTeacherController *controller = [[ViewEvalutionTeacherController alloc] init];
    controller.teacherID = teacherID;
    controller.yyid = yyid;
    [self.navigationController pushViewController:controller animated:true];
}

// 查看取消原因
- (void)showCancelReasonWithyyid:(NSInteger)yyid {
    [SVProgressHUD show];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getReason"
                                        parameters:@{@"s_id": [UserInfoModel shareInstance].stu_id, @"yy_id": @(yyid)}
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               
                                               NSString *reason = respondDictionary[@"reason"];
                                               if (reason == nil || [reason isEqualToString: @""]) {
                                                   reason = @"导师没有说明原因";
                                               }else {
                                                   reason = [NSString stringWithFormat:@"取消原因: %@",reason];
                                               }
                                               [SVProgressHUD showInfoWithStatus:reason];
                                           } failure:^(NSError *error) {
                                               
                                               [SVProgressHUD dismiss];
                                               [SVProgressHUD showErrorWithStatus:@"查看取消原因失败"];
                                           }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        // 预约时间
        return 45;
    } else if(indexPath.section == 1){
        // 老师评价
        return [tableView cellHeightForIndexPath:indexPath model:self.model.evalution[indexPath.row] keyPath:@"model" cellClass:[TeacherEvalutionCell class] contentViewWidth:SCREEN_WIDTH];
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) {
        // 预约时间
        return self.model.times.count;
    } else if(section == 1){
        // 老师评价
        return self.model.evalution.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.section == 0){
        // 已预约时间
        TeacherOrderTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORDCELL"];

        if(cell == nil) {
            cell = [[TeacherOrderTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ORDCELL"];
            cell.needShowDate = true;
        }
        cell.model = self.model.times[indexPath.row];
        return cell;
    } else if (indexPath.section == 1) {
        // 导师评价
        TeacherEvalutionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EVACELL"];

        if(cell == nil) {
            cell = [[TeacherEvalutionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EVACELL"];
        }
        cell.model = self.model.evalution[indexPath.row];
        return cell;

    }

    return nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        // 已预约
        return @"已预约";
    } else {
        // 导师评价
        return @"评价";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:tableView];

    if (indexPath.section == 0) {// 对预约进行操作
        // 取出预约时间模型
        TeacherOrderTimeModel *model = self.model.times[indexPath.row];
        
        // 判断评价导师或是预约\取消导师
        if(model.code == 3 || model.code == 4) {
            // 预约(3)或取消预约(4)
            [self updateOrderTeacherWithModel:model];
        } else if (model.code == 2) {
            // 评价导师
            [self evalutionTeacherWithTeacherID:self.teacherID yyid:model.yyid];
        } else if (model.code == 1) {
            // 查看评价
            [self viewEvalutionWithTeacherID:self.teacherID yyid:model.yyid];
        } else if (model.code == 6) {
            // 导师取消
            [self showCancelReasonWithyyid:model.yyid];
        }
    }


}
@end
